﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI.WebControls;
using blackhouse.WebNemo.Business.Groups;
using blackhouse.WebNemo.Business;
using blackhouse.WebNemo.Web;
using mojoPortal.Business.WebHelpers;

namespace blackhouse.WebNemo.Cms {
    public class EditGroupLink : HyperLink {

        private IGroup group;
        private Factory factory = Factory.Instance();

        public override bool Visible {
            get {
                return base.Visible && (WebUser.IsAdminOrContentAdminOrContentPublisherOrContentAuthor || WebUser.IsInRole("nemo"));
            }
            set {
                base.Visible = value;
            }
        }

        public int GroupId {
            get {
                if (group != null)
                    return this.group.Id;
                else
                    return 0;
            }
            set {
                if (group == null || this.group.Id != value) {
                    this.group = this.factory.GetGroup(value);
                    this.LoadForGroup();
                }
            }
        }

        public string SystemGroupName {
            get {
                if (group != null)
                    return this.group.SystemName;
                else
                    return "";
            }
            set {
                if (group == null || this.group.SystemName != value) {
                    this.group = this.factory.GetGroup(value);
                    this.LoadForGroup();
                }
            }
        }

        protected void LoadForGroup() {
            if (group == null) {
                this.Visible = false;
                return;
            }
            this.Text = "Edytuj " + this.group.Name;
            this.NavigateUrl = NemoUrlUtils.GetUrlUtils().GetDisplayRecords(this.group.Id, NemoUrlUtils.DisplayModFullWidth) + "&refferUrl=" + HttpUtility.UrlEncode(HttpContext.Current.Request.RawUrl);
        }

    }
}